package nl.samenloop.bigscreen.sequence;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import nl.samenloop.bigscreen.Constants;
import nl.samenloop.bigscreen.component.TransparentPanel;
import nl.samenloop.bigscreen.fx.FadeIn;
import nl.samenloop.bigscreen.fx.FadeOut;
import nl.samenloop.bigscreen.fx.FxListener;

public abstract class Sequence {

	protected DrawingTarget target;
	
	public Sequence(DrawingTarget target, String timeTarget) {
		this.target = target;
		setDisplayTime(timeTarget);
	}
	
	// Required time in milliseconds
	private int requiredTime = 5000; 
	
	public abstract void start();
	
	public abstract void prepare();
	
	public String getTitle(String key){
		return Constants.getProperty(key);
	}
	
	protected void setDisplayTime(String key) {
		try {
			requiredTime = Integer.parseInt(Constants.getProperty(key));
			} catch (NumberFormatException nfde) {
				requiredTime = 5000;
			}
	}
	
	protected int getStringWidth(Container c, String s, int size) {
		Graphics g = c.getGraphics();
		
		FontMetrics fm = g.getFontMetrics(new Font("Droid Sans", Font.BOLD, size));
		
		return fm.stringWidth(s);
	}
	
	
	protected JComponent createLabel(String text, int size, boolean wrap) {
		
		FlowLayout fl = new FlowLayout(FlowLayout.CENTER);

		JPanel c = new JPanel(true);
		c.setBackground(Constants.PURPLE);
		// c.setBackground(BigScreen.TRANSPARANT);
		c.setLayout(fl);

		//String msg = String.format("<html><div WIDTH=%d>%s</div><html>", c.getWidth() , text);
		JLabel label = new JLabel(text);
		
		label.setFont(new Font("Droid Sans", Font.BOLD, size));
		//label.setLineWrap(wrap);
		label.setForeground(Color.white);

		c.add(label);

		return c;
	}
	
	private void startPanel(TransparentPanel panel){
		FadeIn.start(panel, 1500);
	}
	
	protected void start(String title) {
		final TransparentPanel panel = (TransparentPanel) target.getCenterContainer();
		target.setTitle(title, new FxListener() {
			
			@Override
			public void finished() {
				startPanel(panel);
				
			}
		});
		
		
	}
	
	public void setRequiredTime(int requiredTime) {
		this.requiredTime = requiredTime;
	}
	
	public int getRequiredTime() {
		return requiredTime;
	}
	
	
	public void stop(){
		final TransparentPanel panel = (TransparentPanel) target.getCenterContainer();
		
		FadeOut.start(panel, 1500, new FxListener() {
			
			@Override
			public void finished() {
				panel.removeAll();
				
			}
		});
	}
	
	
}
